package com.kzd.gtgame.main.mine

import android.content.Intent
import androidx.lifecycle.LifecycleOwner
import com.kzd.gtgame.R
import com.kzd.gtgame.app.GT_CHANNEL
import com.kzd.gtgame.app.GT_FORCE_LOGIN
import com.kzd.gtgame.app.SPSConstance
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.custom.setTransitionStatus
import com.kzd.gtgame.entity.UserInfo
import com.kzd.gtgame.jetpack.vm.MineViewModel
import com.kzd.gtgame.main.AccessAction
import com.kzd.gtgame.main.popup.UserPhonePopup
import com.kzd.gtgame.main.toAccessActivity
import com.module.lemlin.http.Status
import com.module.lemlin.image.loadCircleImage
import com.module.lemlin.owner.OwnerViewModeActivity
import com.zhihu.matisse.Matisse
import kotlinx.android.synthetic.main.activity_mine_user.*
import java.lang.StringBuilder

class MineUserActivity : OwnerViewModeActivity<MineViewModel>() {


    private val mUserPhonePopup: UserPhonePopup by lazy {
        UserPhonePopup(context = this, onClick = {
            toAccessActivity(AccessAction.PHONE_VERIFY)
        })
    }
    private var mImages: MutableList<String> = mutableListOf()

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_user

    override fun stateBar(): StatusBar {
        return StatusBar(color = R.color.white, view = flMineUserBar)
    }

    override fun initView() {

//        fillData(SPSConstance.userInfo)

        flMineUserHeader.setOnClickAntiShakeListener {
            viewModel.userMatisse(activity = this)
        }

        flMineUserNickname.setOnClickAntiShakeListener {
            toAccessActivity(AccessAction.SETTING_NICKNAME)
        }

        flMineUserQQ.setOnClickAntiShakeListener {
            toAccessActivity(AccessAction.SETTING_QQ)
        }

        flMineUserPhone.setOnClickAntiShakeListener {
            if (SPSConstance.userInfo?.mobile.isNullOrEmpty()) {
                toAccessActivity(AccessAction.PHONE_BIND)
            } else {
                mUserPhonePopup.showPopupWindow()
            }
        }

        flMineUserRealName.setOnClickAntiShakeListener {
            if (SPSConstance.userInfo?.idcard.isNullOrEmpty() ||
                SPSConstance.userInfo?.name.isNullOrEmpty() ||
                SPSConstance.userInfo?.verify_info != 1
            ) {
                toAccessActivity(AccessAction.VERIFICATION)
            }
        }

        tvMineUserModifyPwd.setOnClickAntiShakeListener {
            if (SPSConstance.userInfo?.is_pwd == 0) {
                toAccessActivity(AccessAction.PWD_SETTING)
            } else {
                toAccessActivity(AccessAction.PWD_CHANGE)
            }
        }


    }

    override fun initData() {
//        viewModel.userInfo()
    }

    override fun initViewMode() {
        viewModel.userInfoLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            fillData(data)
        })

        viewModel.userUploadLiveData.observe(this as LifecycleOwner, { response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            if (data.size == 0)
                return@observe
            val images = StringBuilder()
            data.forEach {
                images.append(it).append(",")
            }
            viewModel.userChange(face = images.substring(0, images.length - 1).toString())
        })
    }

    override fun onRestart() {
        super.onRestart()

        viewModel.userInfo()
        fillData(SPSConstance.userInfo)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100 && resultCode == RESULT_OK) {
            val pathList: MutableList<String> = Matisse.obtainPathResult(data).toMutableList()
            if (pathList.size == 0)
                return
            viewModel.userUpload(images = pathList)
//            val files = try {
//                Luban.with(OwnerApplication.application).load(pathList).get()
//            } catch (e: IOException) {
//                null
//            }?.map { it.path }?.toMutableList()

//            UCrop.of(Uri.parse("file:/storage/emulated/0/Pictures/ic_90010.png"),Uri.parse("file:/storage/emulated/0/Pictures/ic_90012.png"))
//                .withAspectRatio(16f, 9f)
//                .withMaxResultSize(100, 100)
//                .start(this)
        }
    }

    private fun fillData(userInfo: UserInfo?) {

        ivMineUserHeader.loadCircleImage(
            if (userInfo?.face.isNullOrEmpty())
                R.mipmap.ic_test_1 else userInfo?.face ?: R.mipmap.ic_test_1
        )
        tvMineUserAccount.text = userInfo?.user_name
        tvMineUserNickname.text = if (userInfo?.pet_name.isNullOrEmpty())
            "未设置" else userInfo?.pet_name
        tvMineUserQQ.text = if (userInfo?.qq.isNullOrEmpty())
            "未设置" else userInfo?.qq
        tvMineUserPhone.text = if (userInfo?.mobile.isNullOrEmpty())
            "未绑定" else userInfo?.mobile
        tvMineUserRealName.text = if (userInfo?.idcard.isNullOrEmpty() ||
            userInfo?.name.isNullOrEmpty() ||
            userInfo?.verify_info != 1
        )
            "未认证" else "${userInfo.name},${userInfo.idcard}"
        tvMineUserModifyPwd.text = if (userInfo?.is_pwd == 0)
            "设置登录密码" else "修改登录密码"

        if (userInfo?.user_name.equals("oopp778")) {
            tvMineUserModifyChannel.text = StringBuilder().append(GT_CHANNEL)
                .append(":").append(GT_FORCE_LOGIN)
        }
    }
}